<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" style="width: 100%">
              <el-table-column label="序号" type="index" width="50" />
              <el-table-column prop="name" label="角色名称" width="180" />
              <el-table-column prop="description" label="描述" />
              <el-table-column width="300" label="操作">
                <template #default="{row}">
                  <el-button size="mini" type="success" @click="hAssignPer(row.id)">分配权限</el-button>
                  <el-button size="mini" @click="hEdit(row)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[2,3,5,10,20]"
                :page-size="pageParams.pagesize"
                layout="total,sizes,prev,pager,next,jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog title="新增角色" :visible.sync="showDialog" @close="hResetForm">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限的弹层 -->
      <el-dialog title="分配权限" :visible.sync="showDialogper">
        <assignPermission :id="curId" @close="showDialogper=false" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, addRolesList, updateRolesList, delRolesList } from '@/api/setting'
import assignPermission from './assignPermission'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色的名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色的描述', trigger: 'blur' }
        ]
      },
      pageParams: {
        page: 1,
        pagesize: 2
      },
      list: [],
      total: 0,
      showDialog: false,
      isEdit: false,
      showDialogper: false,
      curId: ''
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    hAssignPer(id) {
      this.curId = id
      this.showDialogper = true
    },
    hResetForm() {
      this.$refs.roleForm.resetFields()
      this.roleFrom = {
        name: '',
        description: ''
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRolesList(id)
        if (this.list.length === 1) {
          this.queryParams.page--
          if (this.queryParams.page <= 0) {
            this.queryParams.page = 1
          }
        }
        this.loadRoles()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hAdd() {
      this.isEdit = false
      this.roleForm = {
        name: '',
        description: ''
      }
      this.showDialog = true
    },
    hEdit(data) {
      this.isEdit = true
      this.roleForm = { ...data }
      this.showDialog = true
    },
    async doAdd() {
      try {
        const res = await addRolesList(this.roleForm)
        console.log(res, '添加')
        this.total++
        this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize)
        this.loadRoles()
        this.showDialog = false
        this.$message.success(res.message)
      } catch (error) {
        console.log(error)
      }
    },
    async doEdit() {
      try {
        await updateRolesList(this.roleForm)
        this.loadRoles()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        console.log(res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    hCurrentChange(curPage) {
      console.log(curPage, '页码')
      this.pageParams.page = curPage
      this.loadRoles()
    },
    handleSizeChange(val) {
      this.queryParams.page = 1
      this.queryParams.pagesize = val
      this.loadRolesList()
    }
  }
}
</script>
